package part_02_Basic_Thread_Synchronization;

import java.util.Scanner;

class Processor extends Thread {

	// so the use of the keyword volatile prevents the threads from caching the
	// values of the variables when they are not changed from within that thread
	volatile boolean flag = true;

	public void run() {

		while (flag) { // this thread kinda tends not to check the value of the
						// flag again and again as it is not expecting any other
						// thread to modify it's data.In order to make this
						// thread check the value of the thread after every
						// run,the use of the keyword volatile is made.Now on
						// each run the check for the value of flag is
						// guaranteed.
			System.out.println("keep printing");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	void stopRunningThread() {

		flag = false;

	}

}

public class App0 {

	public static void main(String[] args) {
		Processor p1 = new Processor();
		p1.start();

		System.out.println("Please press Enter to exit loop");
		Scanner sc = new Scanner(System.in);
		sc.nextLine();

		p1.stopRunningThread();

	}

}
